package com.deli.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class weixinInterface {
    public static void main(String[] args)
            throws Exception {
        sendMessage("QsLTu7ZtrtIoLeo0R42gFkAEVuWzrbf_ZiQVzAdaSX4", "HEY!", 1000053.0D, 1);
    }

    public static String getAccessToken(String secret)
            throws Exception {
        String corpid = "ww76673d8a0d7cd9d7";
        String corpsecret = secret;
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret;
        String result = "";

        String code = "";
        String token = "";
        try {
            HttpGet request = new HttpGet(url);
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
                JSONObject json = JSONObject.parseObject(result);
                System.out.println(result);
                code = json.get("errcode").toString();
                System.out.println(code);
                token = json.get("access_token").toString();
                System.out.println(token);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    public static String sendMessage(String secret, String content, double agentId, int tagId) throws Exception {
        String token = getAccessToken(secret);
        String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token;
        HttpPost httppost = new HttpPost(url);

        JSONObject param = new JSONObject();
        JSONObject message = new JSONObject();
        message.put("content", content);
        param.put("touser", "");
        param.put("toparty", "");
        param.put("totag", Integer.valueOf(tagId));
        param.put("msgtype", "text");
        param.put("agentid", Double.valueOf(agentId));
        param.put("text", message);
        param.put("safe", Integer.valueOf(0));

        String resultpost = "{\"errcode\":1,\"errmsg\":\"failed\",\"invaliduser\":\"\"}";
        try {
            StringEntity entity = new StringEntity(param.toString(), "utf-8");
            httppost.setEntity(entity);
            HttpResponse response = new DefaultHttpClient().execute(httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                resultpost = EntityUtils.toString(response.getEntity());
                return resultpost;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        StringEntity entity;
        return resultpost;
    }
}